<?php
/**
 * 分页类
 *
 * @author cevin<ccevin@qq.com>
 */
class class_pager {
    /**
     * 当前页
     *
     * @var string
     */
    private $page_no = 1;

    /**
     * 总共多少条数据
     *
     * @var string
     */
    private $result_total = 0;

    /**
     * 每页显示几条数据
     *
     * @var string
     */
    private $page_size = 10;

    /**
     * 分页批次
     *
     * @var string
     */
    private $page_limit = 15;

    /**
     * 分页连接URL
     *
     * @var string
     */
    private $url = '';

    /**
     * 分页符号
     *
     * @var string
     */
    private $page_symol = '';

    /**
     * 设置必填数据
     *
     * @param int $page_no 当前页数
     * @param int $page_total 总数据数
     * @param int $page_size 每页显示几条数据
     * @param int $page_limit 分页批次
     *
     * @var function
     */
    public function setConfig($page_no,$result_total,$page_size=10,$page_limit=15) {
        if($page_no == '') $page_no = 1;
        if (!is_numeric($page_no) OR !is_numeric($result_total) OR !is_numeric($page_size) OR !is_numeric($page_limit)) $this->exception('parameters must be number.');

        if ($page_size <= 0) $this->exception('$pagesize val<'.$page_size.'> error.');
        if ($page_no < 1) $page_no = 1;
        
        $this->page_limit = $page_limit;
        $this->page_no = $page_no;
        $this->result_total = $result_total;
        $this->page_size = $page_size;

        return true;
    }

    /**
     * 设置连接URL
     *
     * @param string $url 连接URL 其中$page_symol将被替换成对应的页码
     * @return bool
     */
    public function setUrl($url,$page_symol='Tpage_noT') {
        $this->url = $url;
        $this->page_symol = $page_symol;
    }

    /**
     * 返回分页HTML
     *
     * 分页返回的HTML为<ul><li></li></ul>形式 参数$style指定的为ul的样式
     *
     * @param string $style 分页样式
     * @var function
     */
    public function output($style='') {
        /**
         * 计算总共多少页码
         */
        $page_total = !is_numeric($this->result_total) ? substr($this->result_total,1) : ceil($this->result_total/$this->page_size);

        $dot = '...';
        
        /**
         * 如果总页数大于page_size值则进行分页
         * 否则不分页
         */
        $html = '';
        if ($page_total > 1) {
            $page = $this->page_limit - (strlen($this->page_no)-1);
            if ($page <= 0) $page = 1;
            
            $offset = floor($page * 0.5);

            if ($page > $page_total) {
                $from = 1;
                $to = $page_total;
            } else {
                $from = $this->page_no - $offset;
                $to = $from + $page - 1;
                if ($from < 1) {
                    $to = $this->page_no + 1 - $from;
                    $from = 1;
                    if ($to - $from < $page) $to = $page;
                } elseif($to > $page_total) {
                    $from = $page_total - $page + 1;
                    $to = $page_total;
                }
            }

            $style = $style ? $style : 'cpager';

            $html = '<div class="'.$style.'"><strong>第 '.$this->page_no.' 页，共 '.$page_total.' 页</strong>';
            $html .= ($this->page_no - $offset > 1 && $page_total > $page ? '<a href="##1##">1'.$dot.'</a>' : NULL);
            $html .= $this->page_no > 1 ? '<a href="##'.($this->page_no - 1).'##">&nbsp;&#171;&nbsp;上一页&nbsp;</a>' : NULL;
            for ($i = $from; $i <= $to; $i++) {
                $html .= $i == $this->page_no ? '<a class="current" href="##'.$i.'##">'.$i.'</a>' : '<a href="##'.$i.'##">'.$i.'</a>';
            }
            $html .= ($to < $page_total ? '<a href="##'.$page_total.'##" class="last">'.$dot.$page_total.'</a>' : NULL).($this->page_no < $page_total ? '<a href="##'.($this->page_no + 1).'##" class="next">&nbsp;下一页&nbsp;&#187;&nbsp;</a>' : NULL);
            $html .= '</div>' ; NULL;

            return $this->replaceUrl($html);
        }
        return NULL;
    }

    /**
     * 获取分页Offset值
     *
     * @var function
     */
    public function getOffset() {
        $offset = ((int)$this->page_no-1) * $this->page_size;
        return $offset;
    }
    /**
     * 获取分页Limit值
     *
     * @var function
     */
    public function getLimit() {
        return $this->page_size;
    }

    /**
     * Exception
     *
     * 抛出异常信息
     *
     * @var
     */
    private function exception($msg) {
        throw new Exception("CPager: $msg");
        die;
    }

    /**
     * 替换URL
     *
     * @var function
     */
    private function replaceUrl($html) {
        $html = preg_replace('/##([0-9]+)##/e','$this->replaceUrlCallback(\\1)',$html);
        return $html;
    }
    private function replaceUrlCallback($page_no) {
        $url = str_replace($this->page_symol,$page_no,$this->url);
        return $url;
    }
}